#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initSetting();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initSetting()
{
    model = new QStandardItemModel(2, 6, this);
    selectModel = new QItemSelectionModel(model);

    connect(selectModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(on_TableItemChange(QModelIndex,QModelIndex)));

    model->setRowCount(2);
    model->setColumnCount(6);

    ui->tableView->setModel(model);
    ui->tableView->setSelectionModel(selectModel);
    ui->tableView->setSelectionMode(QAbstractItemView::ExtendedSelection);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectItems);

    ui->tableView->setItemDelegateForColumn(0, &mySpinBoxDelegate);

    setStatusBar("", "", "", "");

}

void MainWindow::setModelData(QStandardItemModel *model, QStringList dataList)
{
    if (dataList.isEmpty() || dataList.length() < 1) return;
    int rowCount = dataList.length() - 1;
    QStringList header = parseTableViewColumn(dataList[0]);
    int columnCount = header.length();

    model->setRowCount(rowCount);
    model->setColumnCount(columnCount);
    model->setHorizontalHeaderLabels(header);

    dataList.removeAt(0);
    setModelItems(model, dataList);
}

void MainWindow::setStatusBar(QString filePath, QString curRow, QString curColumn, QString curValue)
{
    this->curFileLabel = new QLabel(filePath, this);
    this->curRowLabel = new QLabel(curRow, this);
    this->curColumnLabel = new QLabel(curColumn, this);
    this->curValueLabel = new QLabel(curValue, this);

    ui->statusbar->addWidget(curFileLabel);
    ui->statusbar->addWidget(curRowLabel);
    ui->statusbar->addWidget(curColumnLabel);
    ui->statusbar->addWidget(curValueLabel);

}

void MainWindow::setModelItems(QStandardItemModel *model, QStringList dataList)
{
    QStandardItem *line;
    QStringList items;
    int column;
    for (int row = 0; row < dataList.length(); row++) {
        items = parseTableViewColumn(dataList[row]);
        for (column = 0; column < items.length() - 1; column++) {
            line = new QStandardItem(items[column]);
            model->setItem(row, column, line);
        }
        // 最后一列设置为checkable
        line = new QStandardItem(items[column]);
        line->setCheckable(true);
        line->setCheckState(items[column] == "1" ? Qt::Checked : Qt::Unchecked);
        model->setItem(row, column, line);
    }
}

void MainWindow::enableToolButton()
{
    ui->action_add->setEnabled(true);
}


// 加载文件
void MainWindow::on_action_load_triggered()
{
    QString curPath = QCoreApplication::applicationDirPath();
    QString selectFile = QFileDialog::getOpenFileName(this, "打开一个文件", curPath, "*");
    if (selectFile.isEmpty()) return;

    QStringList dataList;
    QFile file(selectFile);
    if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QTextStream fileStream(&file);
        while (!fileStream.atEnd()) {
            QString line = fileStream.readLine();
            ui->plainTextEdit->appendPlainText(line);
            // dataList << line;
            dataList.append(line);
        }
        file.close();
        setModelData(model, dataList);
        // setStatusBar(selectFile, "", "", "");
        enableToolButton();
    }
}

QStringList MainWindow::parseTableViewColumn(QString header)
{
    QStringList headerList;
    if (!header.isEmpty()) {
        headerList = header.split(QRegExp("\\s+"), QString::SkipEmptyParts);
    }

    return headerList;
}

void MainWindow::on_tableView_doubleClicked(const QModelIndex &index)
{

}

void MainWindow::on_tableView_clicked(const QModelIndex &index)
{
    // this->curRowLabel->setText("当前行：" + QString::number(index.row() + 1));
    this->curRowLabel->setText(QString::asprintf("当前行：%d", index.row() + 1));
    this->curColumnLabel->setText("当前列"+ QString::number(index.column() + 1));
    this->curValueLabel->setText("当前值：" + model->itemFromIndex(index)->text());
}

void MainWindow::on_TableItemChange(const QModelIndex &cur, const QModelIndex &pre)
{
    if (cur.isValid()) {
        this->curValueLabel->setText("当前值：" + this->model->itemFromIndex(cur)->text());
    }
}

void MainWindow::on_action_add_triggered()
{
    QModelIndex index = ui->tableView->currentIndex();
    qDebug()<<index.row();
    int insRow = index.row() + 1;
    QStandardItem *item;
    QList<QStandardItem*> list;

    item = new QStandardItem(insRow, 0);
    item->setText("123");
    list.append(item);

    item = new QStandardItem(insRow, 1);
    item->setText("234");
    list.append(item);

    model->insertRow(insRow, list);

}
